import moment from 'moment'
import 'moment/locale/zh-cn'
import { createApp } from 'vue'
import App from '@/App.vue'

// 设置语言环境
moment.locale('zh-cn')
if (window.$vueApp === undefined) {
  window.$vueApp = createApp(App)
}
// 定义全局过滤器
const filters = window.$vueApp.config.globalProperties.$filters ||
    (window.$vueApp.config.globalProperties.$filters = {})

// 数字格式化函数
filters.NumberFormat = function (value) {
  if (typeof value !== 'number') {
    throw new Error('Value must be a number')
  }
  if (!value) {
    return '0'
  }
  return value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
}

// 时间格式化函数
function formatDate (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
}

filters.moment = formatDate
